package cn.kgc.util;

import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;

import java.io.IOException;

/**
 * @author wangyue
 * @version 1.0
 */
public class SensitiveInfoSerializer  extends JsonSerializer<String> implements ContextualSerializer {

    /**
     * 数据脱敏的类型
     */
    private DesensitizationType desensitizationType;

    /**
     * 被脱敏的数据前多少位不要被遮罩
     * 153******78
     */
    private int prefixLen;

    /**
     * 被脱敏的数据后多少位不要被遮罩
     */
    private int suffixLen;

    /**
     * 对属性值进行脱敏
     */
    @Override
    public void serialize(String s, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if(StrUtil.isNotBlank(s)){
            //如果属性值不为空,才需要给属性的进行脱敏
            switch (desensitizationType){
                //如果属性的值是中文名,按照中文名的规则进行脱敏
                case CHINESE_NAME:
                    s= DesensitizedUtil.chineseName(s);
                    break;
                case ID_CARD:
                    s=DesensitizedUtil.idCardNum(s,prefixLen,suffixLen);
                    break;
                case FIXED_PHONE:
                    s=DesensitizedUtil.fixedPhone(s);
                    break;
                case MOBILE_PHONE:
                    s=DesensitizedUtil.mobilePhone(s);
                    break;
                case ADDRESS:
                    s=DesensitizedUtil.address(s,prefixLen);
                    break;
                case EMAIL:
                    s=DesensitizedUtil.email(s);
                    break;
                case PASSWORD:
                    s=DesensitizedUtil.password(s);
                    break;
                case CUSTOMER:
                    // 15312345678  ==> 接取前3位 ==>   153
                    //获得前多少位不遮盖的内容
                   String before= s.substring(0,prefixLen);
                   // 15312345678  ==> 后4位 ==>    11-4=7 从下标为7的位置开始截取到字符串的末尾    ==>5678
                   //获得后多少位不遮盖的内容
                   String after= s.substring(s.length()-suffixLen);
                   //算出中间被遮盖的长度
                    int middle= s.length() - prefixLen-suffixLen;
                   //中间被遮盖的长度应该是被*遮盖
                   String temp= StrUtil.repeat("*",middle);
                   s=before+temp+after;
                   break;
            }
        }
        //将脱敏后的值写入到json中
        jsonGenerator.writeString(s);
    }

    /**
     * 获得属性上使用的脱敏注解的信息
     */
    @Override
    public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) throws JsonMappingException {
        //如果属性不为空
        if(beanProperty!=null){
           //获得属性上使用的脱敏注解
           Desensitization desensitization= beanProperty.getAnnotation(Desensitization.class);
           desensitizationType=desensitization.desensitizationType();
           prefixLen=desensitization.prefixLen();
           suffixLen=desensitization.suffixLen();
        }
        return this;
    }
}
